{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "16773e5b-3873-42ad-b34a-98d2da393efb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "\n",
    "def get_folder_size(folder):\n",
    "    total_size = 0\n",
    "    for dirpath, dirnames, filenames in os.walk(folder):\n",
    "        for filename in filenames:\n",
    "            filepath = os.path.join(dirpath, filename)\n",
    "            if os.path.exists(filepath):\n",
    "                total_size += os.path.getsize(filepath)\n",
    "    return total_size\n",
    "\n",
    "def get_subfolders_size(folder):\n",
    "    subfolder_sizes = {}\n",
    "    for item in os.listdir(folder):\n",
    "        item_path = os.path.join(folder, item)\n",
    "        if os.path.isdir(item_path):\n",
    "            subfolder_sizes[item_path] = get_folder_size(item_path)\n",
    "    return subfolder_sizes\n",
    "    \n",
    "def get_drive_folders_size(drive):\n",
    "    folder_sizes = {}\n",
    "    for item in os.listdir(drive):\n",
    "        item_path = os.path.join(drive, item)\n",
    "        if os.path.isdir(item_path):\n",
    "            folder_size = get_folder_size(item_path)\n",
    "            if folder_size > 1 * 1024**3:  # 如果文件夹大小大于1GB                \n",
    "                folder_sizes[item_path] = get_subfolders_size(item_path)\n",
    "            else:\n",
    "                folder_sizes[item_path] = folder_size\n",
    "    return folder_sizes\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6865b554-f9b4-41fc-8db1-eeef70d793c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe(path):\n",
    "    print(f\"Start to analysis path: {path}\")\n",
    "    start = time.time()\n",
    "    folder_sizes = get_drive_folders_size(path)\n",
    "    flat_folder_size = {}\n",
    "    for folder, size in folder_sizes.items():\n",
    "        if isinstance(size, dict):  # 如果是字典，表示有子文件夹\n",
    "            for subfolder, subsize in size.items():\n",
    "                flat_folder_size[subfolder] = subsize\n",
    "        else:\n",
    "            flat_folder_size[folder] = size\n",
    "    #按照降序排列,取TOP 10\n",
    "    sorted_folder = dict(sorted(flat_folder_size.items(), key=lambda item:item[1], reverse= True)[:10])\n",
    "    for folder, size in sorted_folder.items():\n",
    "         print(f\"Folder: {folder}, size: {size / (1024**2):.2f} MB\")\n",
    "    end = time.time()\n",
    "    print(f\"End to analysis path: {path} ,分析耗时: {(end - start):.2f} 秒 \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "60daa907-0aa3-4550-8137-354866c8ac6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start to analysis path: C:\\Users\\manulism\\AppData\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\Tencent, size: 3717.53 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Local\\Yarn, size: 3088.78 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Local\\JetBrains, size: 1493.40 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Local\\Microsoft, size: 1317.57 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Local\\pip, size: 1083.54 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\nvm, size: 780.07 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Local\\AzureFunctionsTools, size: 598.92 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\DingTalk, size: 596.99 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Local\\npm-cache, size: 544.73 MB\n",
      "Folder: C:\\Users\\manulism\\AppData\\Roaming\\Code, size: 527.45 MB\n",
      "End to analysis path: C:\\Users\\manulism\\AppData ,分析耗时: 213.23 秒 \n"
     ]
    }
   ],
   "source": [
    "drive = 'C:\\\\Users\\manulism\\AppData' \n",
    "describe(drive)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faf6f02d-b55f-4274-9ef9-cb43552fbe9c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
